************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 13 (Treatment Effects on Attention to Work Incentives)
* Last edited: 07 May 2021
************************************************
	
************************************************	
*0. Initial Setup
************************************************

		clear all
		set more off
		set matsize 800
		
************************************************
***** SALIENCE REGRESSIONS  ********************
************************************************

use "$d/salience_dataset.dta", clear

preserve
rename output_section output

forval j_sample = 0/2 {
*sample = 0: morning
*sample = 1: afternoon
*sample = 2: overall
	
	foreach var in output s_typing_min {

	*** Full-sample regression

	qui reghdfe `var' high treat_high nap_high salience high_sal_treat high_sal_nap high_sal ///
	if post_treatment==1 & new_vers>=0 & sample_`j_sample' == 1, ///
	absorb(day_in_study pid date) vce(cluster pid)

	local n_pids_`j_sample' = e(N_clust)
	local n_obs_`j_sample' = e(N)

	* Control    
	 nlcom (_b[high] ) /(_b[high_sal]+ _b[high])
	 mat def B = r(b)
	 mat def V = r(V)
	 local control_coef_`var'_`j_sample' = string(B[1,1], "%3.2f")
	 local control_se_`var'_`j_sample' = string(sqrt(V[1,1]), "%3.2f")
	 
	* Nap
	 nlcom (_b[high] + _b[nap_high]) /(_b[high_sal]+ _b[high]+ _b[nap_high] + _b[high_sal_nap])
	 mat def B = r(b)
	 mat def V = r(V)
	 local nap_coef_`var'_`j_sample' = string(B[1,1], "%3.2f")
	 local nap_se_`var'_`j_sample' = string(sqrt(V[1,1]), "%3.2f")

	* Night-sleep
	 nlcom (_b[high] + _b[treat_high]) /(_b[high_sal]+ _b[high]+ _b[treat_high] + _b[high_sal_treat])
	 mat def B = r(b)
	 mat def V = r(V)
	 local treat_coef_`var'_`j_sample' = string(B[1,1], "%3.2f")
	 local treat_se_`var'_`j_sample' = string(sqrt(V[1,1]), "%3.2f")
	 
	* Nap - Control (p-val)
	 
	 nlcom  (_b[high] + _b[nap_high]) /(_b[high_sal]+ _b[high]+ _b[nap_high] + _b[high_sal_nap]) ///
	 - (_b[high] ) /(_b[high_sal]+ _b[high])
	 mat def B = r(b)
	 mat def V = r(V)
	 local p_nap_control_`var'_`j_sample' = string((2 * ttail(e(df_r), abs(B[1,1]/sqrt(V[1,1])))), "%9.2f")
	 
	* Night-Sleep - Control (p-val)
	 
	 nlcom  (_b[high] + _b[treat_high]) /(_b[high_sal]+ _b[high]+ _b[treat_high] + _b[high_sal_treat]) ///
	 - (_b[high] ) /(_b[high_sal]+ _b[high])
	 mat def B = r(b)
	 mat def V = r(V)
	 local p_treat_control_`var'_`j_sample' = string((2 * ttail(e(df_r), abs(B[1,1]/sqrt(V[1,1])))), "%9.2f")

	}
}


** Exporting table **
		cd "$oa/Tables"
		file open f using "TableA13_salience.tex", write replace
			file write f "\begin{tabular*}{1\textwidth}{l@{\extracolsep{\fill}}*{6}{c}}" _n ///
			"\toprule" _n ///
			"& \multicolumn{2}{c}{\textbf{Overall}} & \multicolumn{2}{c}{\textbf{Morning}} & \multicolumn{2}{c}{\textbf{Afternoon}}  \\" _n ///
			"\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7} & Output & Minutes & Output & Minutes & Output & Minutes \\" _n ///
			" & (1) &(2) & (3) & (4) & (5) & (6) \\" _n ///
			"\midrule" _n ///
			"Night-Sleep Treatments & `treat_coef_output_2' & `treat_coef_s_typing_min_2' & `treat_coef_output_0' & `treat_coef_s_typing_min_0' & `treat_coef_output_1' & `treat_coef_s_typing_min_1'\\" _n ///
			"& (`treat_se_output_2') & (`treat_se_s_typing_min_2') & (`treat_se_output_0') & (`treat_se_s_typing_min_0') & (`treat_se_output_1') & (`treat_se_s_typing_min_1') \\" _n ///
			"Nap Treatment  & `nap_coef_output_2' & `nap_coef_s_typing_min_2' & `nap_coef_output_0' & `nap_coef_s_typing_min_0' & `nap_coef_output_1' & `nap_coef_s_typing_min_1'  \\" _n ///
			"& (`nap_se_output_2') & (`nap_se_s_typing_min_2') & (`nap_se_output_0') & (`nap_se_s_typing_min_0') & (`nap_se_output_1') & (`nap_se_s_typing_min_1') \\" _n ///
			"Control & `control_coef_output_2' & `control_coef_s_typing_min_2' & `control_coef_output_0' & `control_coef_s_typing_min_0' & `control_coef_output_1' & `control_coef_s_typing_min_1' \\" _n ///
			"& (`control_se_output_2') & (`control_se_s_typing_min_2') & (`control_se_output_0') & (`control_se_s_typing_min_0') &(`control_se_output_1') & (`control_se_s_typing_min_1')  \\" _n ///
			"\midrule" _n ///
			"\textit{p}-value NS vs. Control & `p_treat_control_output_2' & `p_treat_control_s_typing_min_2' & `p_treat_control_output_0' & `p_treat_control_s_typing_min_0' & `p_treat_control_output_1' & `p_treat_control_s_typing_min_1'  \\" _n ///		
			"\textit{p}-value Nap vs. Control & `p_nap_control_output_2' & `p_nap_control_s_typing_min_2' & `p_nap_control_output_0' & `p_nap_control_s_typing_min_0' & `p_nap_control_output_1' & `p_nap_control_s_typing_min_1' \\" _n ///
			"Participants & `n_pids_2' & `n_pids_2' & `n_pids_0' & `n_pids_0' & `n_pids_1' & `n_pids_1' \\" _n /// 
			"\bottomrule" _n ///
			"\end{tabular*}" _n
		file close f

